$().ready(function() {
	$('.summernote').summernote({
		height : '220px',
		lang : 'zh-CN',
        callbacks: {
            onImageUpload: function(files, editor, $editable) {
                sendFile(files);
            }
        }
    });
	var content = $("#content").val();
	$('#content_sn').summernote('code', content);
	validateRule();
});

$.validator.setDefaults({
	ignore:"", // 避免hidden不被做校验
	submitHandler : function() {
        save();
	}
});
function save(status) {
	$("#status").val(status);
	var content_sn = $("#content_sn").summernote('code');
	$("#content").val(content_sn);
	$.ajax({
		cache : true,
		async : false,
		type : "POST",
		url : "/blog/bContent/save",
		dataType: "JSON",
		contentType: "application/json;charset=UTF-8",
		data : JSON.stringify({
			"cid": $("#cid").val(),
			"cover": $("#cover").val(),
			"sourceCode": $("#sourceCode").val(),
			"sourceCodeFileName": $("#sourceCodeFileName").val(),
			"title": $("#title").val(),
			"author": $("#author").val(),
			"content": $("#content").val(),
			"categories": $("#categories").val(),
			"blogLabelIds": layui.formSelects.value('blogLabel_select', 'val'),
			"allowComment": $("#allowComment").val(),
			"allowFeed": $("#allowFeed").val(),
			"status": $("#status").val()
		}),
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
			} else {
				parent.layer.alert(data.msg)
			}
		}
	});

}
function validateRule() {
	var content_sn = $("#content_sn").summernote('code');
	$("#content").val(content_sn);
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
        rules: {
            title: {
                required: true
            },
            author:{
                required: true
            },
            content:{
                required: true
            },
            cover:{
                required: true
            },
        },
        messages: {
            title: {
                required: icon + "请填写文章标题"
            },
            author: {
                required: icon + "请填写文章作者"
            },
            content: {
                required: icon + "请填写文章内容"
            },
            cover: {
                required: icon + "请上传文章封面"
            }
        }
	})
}

function returnList() {
	var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
	parent.layer.close(index);
}


layui.use('upload', function () {
	var upload = layui.upload;
	// 封面上传
	var coverUpload = upload.render({
		elem: '#uploadCoverButton', //绑定元素
		url: '/common/sysFile/upload', //上传接口
		size: 60000,
		accept: 'file',
		done: function (r) {
			if (r.code == 0) {
				layer.msg(r.msg);
				$("#cover").val(r.filePath)
				$('#coverImg').attr('src', r.fileSysPath); //图片链接（base64）
			}else {
				layer.msg(r.msg);
			}
		},
		error: function (r) {
			layer.msg(r.msg);
		}
	});

	// 源码上传
	var sourceCodeUpload = upload.render({
		elem: '#uploadSourceCodeButton', //绑定元素
		url: '/common/sysFile/upload', //上传接口
		size: 60000,
		accept: 'file',
		done: function (r) {
			if (r.code == 0) {
				layer.msg(r.msg);
				$("#sourceCode").val(r.filePath);
				$("#sourceCodeFileName").val(r.fileName);
				$("#show_sourceCode").text(r.fileName);
			}else {
				layer.msg(r.msg);
			}
		},
		error: function (r) {
			layer.msg(r.msg);
		}
	});
});


/**
 * 生成随机密码
 */
function randomPassWord() {
	$.ajax({
		cache : true,
		type : "POST",
		url : "/blog/bContent/randomPassWord",
		data : {},// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(r) {
			if (r.code == 0) {
				layer.open({
					area: ['500px', '200px'],
					content: '<label>密码：'+ r.randomPassWord.password +'</label><br/>' +
						'<label>公钥：'+ r.randomPassWord.aesPassword +'</label>',
					btn: '我知道了'
				});
			} else {
				parent.layer.alert(r.msg)
			}
		}
	});
}
